<?php

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

function resizeIMG($is_thmb, $src_file, $dest_file, $width, $height, $prop, $quality) {
    global $eaconf,$mosConfig_live_site, $mosConfig_absolute_path;
    	
    $imagetype = array( 1 => 'GIF', 2 => 'JPG', 3 => 'PNG' );
    $imginfo = getimagesize($src_file);

    if ($imginfo == null) return _EAB_GL_NOFILE;

    $imginfo[2] = $imagetype[$imginfo[2]];

    // GD can only handle JPG & PNG images
    if ($imginfo[2] != 'JPG' && $imginfo[2] != 'GIF' &&  $imginfo[2] != 'PNG' )
        return "ERROR: GD can only handle JPG and PNG files!";

    // source height/width
    $srcWidth = $imginfo[0];
    $srcHeight = $imginfo[1];
		
    if ($prop == 1) {
        $wantratio = $width / $height;
        $haveratio = $srcWidth / $srcHeight;
	
        if ($wantratio < $haveratio) {
            $destWidth = $width;
            $destHeight = $width / $haveratio;
        } else {
            $destWidth = $height * $haveratio;
            $destHeight = $height;
        }
    } else {
        $destWidth = (int)($width);
  	$destHeight = (int)($height);
    }
			
    if (!function_exists('imagecreatefromjpeg')) return _EAB_GL_NOGD;

    if ($imginfo[2] == 'JPG') $src_img = imagecreatefromjpeg($src_file);
    else
        if ($imginfo[2] == 'GIF') $src_img = imagecreatefromgif($src_file);
    	else
            if ($imginfo[2] == 'PNG') $src_img = imagecreatefrompng($src_file);

    if (!$src_img) return _EAB_GL_NOGD;

    if (function_exists("imagecreatetruecolor"))
        $dst_img = imagecreatetruecolor($destWidth, $destHeight);
    else
        $dst_img = imagecreate($destWidth, $destHeight);

    if (function_exists("imagecopyresampled"))
        imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, (int)$destWidth, (int)$destHeight, $srcWidth, $srcHeight);
    else
        imagecopyresized($dst_img, $src_img, 0, 0, 0, 0,(int) $destWidth, (int)$destHeight, $srcWidth, $srcHeight);
	
    if (!$is_thmb && $eaconf->ea_watermark) {
        $wmstr = substr($mosConfig_live_site,7);
        $wmstr = "(c)" . date("Y") . " " . $wmstr;
        $ftcolor = imagecolorallocate($dst_img,239,239,239);
        imagestring ($dst_img, 2,10, $destHeight-20, $wmstr, $ftcolor);
    }

    imagejpeg($dst_img, $dest_file, $quality);
    imagedestroy($src_img);
    imagedestroy($dst_img);

    // Set mode of uploaded picture
    chmod($dest_file, octdec('644'));

    // We check that the image is valid
    $imginfo = getimagesize($dest_file);
    if ($imginfo == null) return _EAB_GL_FCD;

    return;
  }		
?>